from pygame.sprite import Sprite
import pygame
from point import Point
from arkanoid_errors import UserDisq
class Ball (Sprite):
    mouse_clicked = False
    point = Point()
    previous_x = None
    previous_y = None
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image = pygame.image.load('ball.png').convert()
        self.image.set_colorkey(self.image.get_at((0,0)))
        self.rect = self.image.get_rect()
        self.rect = self.rect.move((25,370));
    
    def update(self):
        pass
    
    def move(self, event):
        if self.mouse_clicked == False and event != None:
            x,y = event.pos
            if ( x < 800 - self.rect.width - 60 ):
                self.rect = pygame.Rect(x + 25, 370, self.rect.width, self.rect.height)
        if self.mouse_clicked and event == None:
            self.handle_collisions()
            self.previous_x = self.point.x
            self.previous_y = self.point.y
            self.point.move()
            self.rect = pygame.Rect(self.point.x, self.point.y, self.rect.width, self.rect.height)
            
    def handle_collisions(self):
        if self.point.y >= 400-self.rect.height:
            raise UserDisq()
        if self.point.x >= ( 800 - self.rect.width )  or self.point.x <= 0:
            self.point.collide_x()
        if self.point.y >= ( 400 - self.rect.height ) or self.point.y <= 0:
            self.point.collide_y()
            
    def collide_item( self, bar_velocity ):
        print("collide_item")
        
    
    def move_up(self, bar_velocity ):
        self.point.move_up( bar_velocity )
        
    def collide_cube(self, cube):
        changed_direction = False
        # lets try to figure out which wall did we hit
        # x axis
        left_wall = cube.rect.x - self.rect.width
        right_wall = cube.rect.x + cube.rect.width;
        if ( self.previous_x <= left_wall and self.point.x >= left_wall):
            self.point.collide_x()
            changed_direction = True
        if ( self.previous_x >=  right_wall and self.point.x <= right_wall):
            self.point.collide_x()
            changed_direction = True
            
        # y axis
        top_wall = cube.rect.y - self.rect.height
        bottom_wall = cube.rect.y + cube.rect.height
        if ( self.previous_y >= bottom_wall and self.point.y <= bottom_wall):
            self.point.collide_y()
            changed_direction = True
        if ( self.previous_y <= top_wall and self.point.y >= top_wall):
            self.point.collide_y()
            changed_direction = True
        if not changed_direction:
            print("problem colliding. couldn't decide which direction")
            print("previous_y=%s ,y=%s, previous_x=%s, x=%s, left_wall = %s, right_wall = %s, bottom_wall = %s, top_wall = %s " %(self.previous_y, self.point.y ,self.previous_x, self.point.x, left_wall, right_wall, bottom_wall, top_wall))
        
        
    def handle_mouse_click(self, clicked):
        self.mouse_clicked = clicked
        if ( clicked ):
            self.point.x = self.rect.x
            self.point.y = self.rect.y
        else:
            self.point = Point()
        
        